


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > Unit</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/idea.min.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.min.js"></script>
  <script type="text/javascript" src="../../js/highlightjs-line-numbers.min.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.board</a>
</div>

<h1>Coverage Summary for Class: Unit (nl.tudelft.jpacman.board)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">Unit</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    70%
  </span>
  <span class="absValue">
    (7/10)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    62.1%
  </span>
  <span class="absValue">
    (18/29)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<code class="sourceCode" id="sourceCode">&nbsp;package nl.tudelft.jpacman.board;
&nbsp;
&nbsp;import nl.tudelft.jpacman.sprite.Sprite;
&nbsp;
&nbsp;/**
&nbsp; * A unit that can be placed on a {@link Square}.
&nbsp; *
&nbsp; * @author Jeroen Roosen 
&nbsp; */
<b class="fc">&nbsp;public abstract class Unit {</b>
&nbsp;
&nbsp;    /**
&nbsp;     * The square this unit is currently occupying.
&nbsp;     */
&nbsp;    private Square square;
&nbsp;
&nbsp;    /**
&nbsp;     * The direction this unit is facing.
&nbsp;     */
&nbsp;    private Direction direction;
&nbsp;
&nbsp;    /**
&nbsp;     * Creates a unit that is facing east.
&nbsp;     */
<b class="fc">&nbsp;    protected Unit() {</b>
<b class="fc">&nbsp;        this.direction = Direction.EAST;</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Sets this unit to face the new direction.
&nbsp;     * @param newDirection The new direction this unit is facing.
&nbsp;     */
&nbsp;    public void setDirection(Direction newDirection) {
<b class="fc">&nbsp;        this.direction = newDirection;</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Returns the current direction this unit is facing.
&nbsp;     * @return The current direction this unit is facing.
&nbsp;     */
&nbsp;    public Direction getDirection() {
<b class="nc">&nbsp;        return this.direction;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Returns the square this unit is currently occupying.
&nbsp;     * Precondition: &lt;code&gt;hasSquare()&lt;/code&gt;.
&nbsp;     *
&nbsp;     * @return The square this unit is currently occupying.
&nbsp;     */
&nbsp;    public Square getSquare() {
<b class="fc">&nbsp;        assert invariant();</b>
<b class="fc">&nbsp;        assert square != null;</b>
<b class="fc">&nbsp;        return square;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Returns whether this unit is currently on  a square.
&nbsp;     *
&nbsp;     * @return True iff the unit is occupying a square at the moment.
&nbsp;     */
&nbsp;    public boolean hasSquare() {
<b class="fc">&nbsp;        return square != null;</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Occupies the target square iff this unit is allowed to as decided by
&nbsp;     * {@link Square#isAccessibleTo(Unit)}.
&nbsp;     *
&nbsp;     * @param target
&nbsp;     *            The square to occupy.
&nbsp;     */
&nbsp;    public void occupy(Square target) {
<b class="fc">&nbsp;        assert target != null;</b>
&nbsp;
<b class="fc">&nbsp;        if (square != null) {</b>
<b class="fc">&nbsp;            square.remove(this);</b>
&nbsp;        }
<b class="fc">&nbsp;        square = target;</b>
<b class="fc">&nbsp;        target.put(this);</b>
<b class="fc">&nbsp;        assert invariant();</b>
<b class="fc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Leaves the currently occupying square, thus removing this unit from the board.
&nbsp;     */
&nbsp;    public void leaveSquare() {
<b class="nc">&nbsp;        if (square != null) {</b>
<b class="nc">&nbsp;            square.remove(this);</b>
<b class="nc">&nbsp;            square = null;</b>
&nbsp;        }
<b class="nc">&nbsp;        assert invariant();</b>
<b class="nc">&nbsp;    }</b>
&nbsp;
&nbsp;    /**
&nbsp;     * Tests whether the square this unit is occupying has this unit listed as
&nbsp;     * one of its occupiers.
&nbsp;     *
&nbsp;     * @return &lt;code&gt;true&lt;/code&gt; if the square this unit is occupying has this
&nbsp;     *         unit listed as one of its occupiers, or if this unit is currently
&nbsp;     *         not occupying any square.
&nbsp;     */
&nbsp;    protected boolean invariant() {
<b class="fc">&nbsp;        return square == null || square.getOccupants().contains(this);</b>
&nbsp;    }
&nbsp;
&nbsp;    /**
&nbsp;     * Returns the sprite of this unit.
&nbsp;     *
&nbsp;     * @return The sprite of this unit.
&nbsp;     */
&nbsp;    public abstract Sprite getSprite();
&nbsp;
&nbsp;    /**
&nbsp;     * A utility method for implementing the ghost AI.
&nbsp;     *
&nbsp;     * @param amountToLookAhead the amount of squares to follow this units direction in.
&nbsp;     * @return The square amountToLookAhead spaces in front of this unit.
&nbsp;     */
&nbsp;    public Square squaresAheadOf(int amountToLookAhead) {
<b class="nc">&nbsp;        Direction targetDirection = this.getDirection();</b>
<b class="nc">&nbsp;        Square destination = this.getSquare();</b>
<b class="nc">&nbsp;        for (int i = 0; i &lt; amountToLookAhead; i++) {</b>
<b class="nc">&nbsp;            destination = destination.getSquareAt(targetDirection);</b>
&nbsp;        }
&nbsp;
<b class="nc">&nbsp;        return destination;</b>
&nbsp;    }
&nbsp;}
</code>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
      hljs.highlightAll()
      hljs.initLineNumbersOnLoad();
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2023-04-18 17:14</div>
</div>
</body>
</html>
